//----------------------------------------------------------------------------
// Copyright, 1991 .. 2001, by Mettler & Fuchs AG, Dietikon, Switzerland
//----------------------------------------------------------------------------
//
// Purpose:  Example Program 1 for RTX-51
//
//           Shows:  - task declaration with C language extensions
//                   - system startup
//                   - task creation
//                   - mailbox usage
//                   - use of tasks of different priority
//                   - task switching
//                   - message passing
//
//           This program is intended as an introduction to working with
//           RTX-51.  Feel free to change it in any way helping you to
//           understand RTX-51 operation.
//
// File:     Example1.c
// Created:  1991 by --
// Modified: 2001-JUN-28 by P. Baessler
//
//----------------------------------------------------------------------------

// Includes ------------------------------------------------------------------

#include <Rtx51.h>


// Defines -------------------------------------------------------------------

//
// RTX constants
//
#define  RTX_DONE      0
#define  WAIT_FOREVER  0xFF

//
// Task numbers
//
// These numbers are used in the task declaration and identify the task code
// for task creation.
//
#define  PRODUCER_TASK  0  // Task number for the producer task.
#define  CONSUMER_TASK  1  // Task number for the consumer task.

//
// Task priorities
//
// The initial configuration of this example assigns to the consumer task a
// priority above the producer task. With this priority setting, each message
// produced by the producer will immediately get consumed by the consumer.
//
// Modify the priority constants below and observe what happens
//
// - if the producer priority is higher than the consumer priority
// - both producer and consumer have the same priority
//
#define  PRODUCER_PRIORITY  0
#define  CONSUMER_PRIORITY  1

//
// Mailbox
//
#define  MAILBOX  0


// Tasks --------------------------------------------------------------------

void  Producer (void) _task_ PRODUCER_TASK _priority_ PRODUCER_PRIORITY
//
// Purpose:        This task increments a counter and passes its value to the
//                 mailbox. This is the producer task.
//
{
   unsigned int  MessageToSend = 1;


   for (;;)  // Endless loop of the task
   {
      //
      // Send the actual value of TheMessage to the mailbox until the
      // mailbox is full.
      //
      os_send_message (MAILBOX, MessageToSend, WAIT_FOREVER);
      MessageToSend++; // Increment MessageToSend's value for the next
                       // transmission.
   } // for
} // Producer


void  Consumer (void) _task_ CONSUMER_TASK _priority_ CONSUMER_PRIORITY
//
// Purpose: This task starts the message producer task and waits afterwards
//          for messages from the producer. It waits until a new message
//          arrives when the mailbox is empty.
//
{
   unsigned int   ReceivedMessage;
   unsigned char  RtxCompletion;


   RtxCompletion = os_create_task (PRODUCER_TASK);

   if (RTX_DONE == RtxCompletion)
   {
      for (;;)
      {
         os_wait (K_MBX + MAILBOX, WAIT_FOREVER, &ReceivedMessage);
         //
         // Process the message received ...
         //
      } // for
   }
   else
   {
      for (;;);  // Producer task creation failed. Wait for doomsday.
   } // if

} // Consumer


void  main (void)
//
// Purpose: The main function, which gets executed after the start-up code.
//          It starts the first RTX task and will not return from the
//          os_start_system call provided the task was successfully started.
//
{
   signed char  RtxCompletion;   // RTX completion code

   //
   // Start the real-time system
   //
   RtxCompletion = os_start_system (CONSUMER_TASK);

   //
   // The system could not get started when the program reaches here. Wait for
   // doomsday.
   //
   for (;;);

} // main


